package com.eshop.modules.activity.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.api.EshopException;
import cn.hutool.core.util.StrUtil;
import com.eshop.enums.CouponEnum;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.activity.domain.StoreCoupon;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreCouponQueryCriteria;
import com.eshop.modules.activity.service.StoreCouponService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:优惠券管理" })
@RestController
@RequestMapping({ "api" })
public class StoreCouponController
{
    private final StoreCouponService storeCouponService;
    
    public StoreCouponController(final StoreCouponService storeCouponService) {
        this.storeCouponService = storeCouponService;
    }
    
    @Log("查询")
    @ApiOperation("查询")
    @GetMapping({ "/yxStoreCoupon" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPON_ALL','YXSTORECOUPON_SELECT')")
    public ResponseEntity getStoreCoupons(final StoreCouponQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.storeCouponService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("新增")
    @ApiOperation("新增")
    @PostMapping({ "/yxStoreCoupon" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPON_ALL','YXSTORECOUPON_CREATE')")
    public ResponseEntity create(@Validated @RequestBody final StoreCoupon resources) {
        if (CouponEnum.TYPE_1.getValue().equals(resources.getType()) && StrUtil.isEmpty((CharSequence)resources.getProductId())) {
            throw new EshopException("请选择商品");
        }
        if (resources.getCouponPrice().compareTo(resources.getUseMinPrice()) >= 0) {
            throw new EshopException("优惠券金额不能高于最低消费金额");
        }
        return new ResponseEntity(this.storeCouponService.save(resources), HttpStatus.CREATED);
    }
    
    @Log("修改")
    @ApiOperation("修改")
    @PutMapping({ "/yxStoreCoupon" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPON_ALL','YXSTORECOUPON_EDIT')")
    public ResponseEntity update(@Validated @RequestBody final StoreCoupon resources) {
        if (CouponEnum.TYPE_1.getValue().equals(resources.getType()) && StrUtil.isEmpty((CharSequence)resources.getProductId())) {
            throw new EshopException("请选择商品");
        }
        if (resources.getCouponPrice().compareTo(resources.getUseMinPrice()) >= 0) {
            throw new EshopException("优惠券金额不能高于最低消费金额");
        }
        this.storeCouponService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除")
    @ApiOperation("删除")
    @DeleteMapping({ "/yxStoreCoupon/{id}" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPON_ALL','YXSTORECOUPON_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer id) {
        this.storeCouponService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
